package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.pojo.DtsOrder;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService{
    @Autowired
    private DtsOrderMapper orderMapper;
    @Override
    public int countOrder() {
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        int count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<DayStatis> incrCount() {
        return orderMapper.staticsIncresOrderCnt();
    }

    @Override
    public List<CategorySellAmts> categoryCount() {
        return orderMapper.categorySell();
    }

    @Override
    public IPage<DtsOrder> queryBrandPage( Integer userId, String orderSn,Integer orderStatus, Integer page, Integer limit, String sort, String order) {
        //1. 创建分页对象
        //参数1: 当前页, 参数2: 每页显示条数
        IPage<DtsOrder> ipage = new Page<>(page, limit);

        //2. 封装查询对象
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);

        //判断查询条件不为空拼接查询条件
        if (!StringUtils.isEmpty(orderSn)) {
            queryWrapper.eq("orderSn", orderSn);
        }
        if (userId!=null) {
            queryWrapper.eq("userId", userId);
        }
        if (orderStatus!=null) {
            queryWrapper.eq("orderStatus", orderStatus);
        }



        //排序
        if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
            //判断排序方式是否为升序
            if (order.equalsIgnoreCase("asc")) {
                queryWrapper.orderByAsc(sort);
            } else {
                //降序
                queryWrapper.orderByDesc(sort);
            }
        }


        //3. 查询并返回结果
        IPage<DtsOrder> resultPage = orderMapper.selectPage(ipage, queryWrapper);


        return resultPage;
    }


}
